package com.astrocsr.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.astrocsr.common.Constants;
import com.astrocsr.common.util.DateUtil;
import com.astrocsr.domain.bean.TblCSRLog;
import com.astrocsr.domain.bean.TblGroup;
import com.astrocsr.domain.condition.CSRLogCondition;
import com.astrocsr.domain.condition.SecCondition;
import com.astrocsr.domain.condition.UserCondition;
import com.astrocsr.domain.db.PagingInfo;
import com.astrocsr.domain.db.SearchResult;
import com.astrocsr.domain.web.CSRLogInfo;

@Service("auditLogManager")
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public class AuditLogManager extends BaseManager {  

	public void addAuditlog(
			String actType, 	//0
			String actCode, 	//1 
			String actName, 	//2
			String loginId, 	//3
			String loginRole, 	//4
			String auditMsg,	//5
			String remoteIP		//6
		) throws Exception {

		// insert Log 
		CSRLogInfo logInfo = new CSRLogInfo();
		Date dtnow = new Date();
		logInfo.setActivityType(actType);//Max 20
		logInfo.setActivitycode(actCode);//Max 10
		logInfo.setActivityname(actName);//Max 32

		logInfo.setAcctid("");
		logInfo.setStmtid("");
		logInfo.setStmtdt("");
		logInfo.setUserid(loginId);
		logInfo.setRole(loginRole);
		logInfo.setDate1(dtnow);
		logInfo.setDate2(dtnow);
		logInfo.setStatus(Constants.LOG_STATUS_NEW);
		logInfo.setUserfield1(auditMsg);
		logInfo.setUserfield2("");
		logInfo.setUserfield3("");
		logInfo.setUserfield4("");
		logInfo.setIpaddr(remoteIP);
		addAuditlog(logInfo);
	}
	public void addAuditlog(CSRLogInfo logInfo) throws Exception {

		if (logInfo != null) {
			java.util.Date actionDate 		= new java.util.Date();

			// Declare bean object
			TblCSRLog tblCSRLog = new TblCSRLog(); 
			tblCSRLog.setActivityType(logInfo.getActivityType()); 
			tblCSRLog.setActivitycode(logInfo.getActivitycode()); 
			tblCSRLog.setActivityname(logInfo.getActivityname());
			
			tblCSRLog.setUserid(StringUtils.isNotBlank(logInfo.getUserid())? logInfo.getUserid().toUpperCase(): "");
			
			tblCSRLog.setAcctid(logInfo.getAcctid());
			tblCSRLog.setAccttype(logInfo.getAccttype()); 
			tblCSRLog.setActivitydt(actionDate);
			tblCSRLog.setDate1(logInfo.getDate1());
			tblCSRLog.setDate2(logInfo.getDate2());
			tblCSRLog.setIpaddr(logInfo.getIpaddr());
			tblCSRLog.setRole(logInfo.getRole());
			tblCSRLog.setStatus(logInfo.getStatus());
			tblCSRLog.setStmtdt(logInfo.getStmtdt());
			tblCSRLog.setStmtid(logInfo.getStmtid());
			tblCSRLog.setUserfield1(logInfo.getUserfield1());
			tblCSRLog.setUserfield2(logInfo.getUserfield2());
			tblCSRLog.setUserfield3(logInfo.getUserfield3());
			tblCSRLog.setUserfield4(logInfo.getUserfield4());
			
			getCsrLogDAO().addAuditlog(tblCSRLog); 
		}
	} 
	
	public SearchResult<CSRLogInfo> listAuditLog(String currentGroup,
												 Date startDate,
												 Date endDate,
												 String searchui,
												 String activityType,
												 PagingInfo pagingInfo) throws Exception {

		SearchResult<CSRLogInfo> listAuditlogInfo = null;
		CSRLogInfo csrLogInfo = null;

		CSRLogCondition condition = new CSRLogCondition(startDate, endDate, searchui, activityType);
		if (StringUtils.isNotBlank(currentGroup)) {
			// get all groups that are children of currentGroup
			List<TblGroup> groupList = getGroupDAO().listGroupByParentCode(new SecCondition(currentGroup), null, true);
			if (groupList != null && !groupList.isEmpty()) {
				List<String> groupCodeList = new ArrayList<String>();
				for (TblGroup tblGroup : groupList) {
					groupCodeList.add(tblGroup.getGroupCode());
				}
				condition.setGroupCodeList(groupCodeList);
			} else {
				// in case, don't have any sub group
				return null;
			}
		}
		

		SearchResult<TblCSRLog> listCSRLog = getCsrLogDAO().listAuditLog(condition, pagingInfo);

		TblCSRLog tblCSRLog = null;

		if (listCSRLog != null && listCSRLog.size() > 0) {
			listAuditlogInfo = new SearchResult<CSRLogInfo>();
			for (int i = 0; i < listCSRLog.size(); i++) {
				tblCSRLog = listCSRLog.get(i);

				// copy fields to display
				csrLogInfo = new CSRLogInfo(); 
				csrLogInfo.setAcctid(tblCSRLog.getAcctid());
				csrLogInfo.setAccttype(tblCSRLog.getAccttype());
				csrLogInfo.setAcctno(tblCSRLog.getAcctno());
				csrLogInfo.setActivitycode(tblCSRLog.getActivitycode());
				csrLogInfo.setActivitydt(tblCSRLog.getActivitydt());
				csrLogInfo.setActivityDate(DateUtil.formatLocalDashTime(tblCSRLog.getActivitydt()));
				//csrLogInfo.setActivityid(tblCSRLog.getActivityid());
				csrLogInfo.setActivityname(tblCSRLog.getActivityname());
				csrLogInfo.setDate1(tblCSRLog.getDate1());
				csrLogInfo.setDate2(tblCSRLog.getDate2());
				csrLogInfo.setIpaddr(tblCSRLog.getIpaddr());
				csrLogInfo.setRole(tblCSRLog.getRole());
				csrLogInfo.setStatus(tblCSRLog.getStatus());
				csrLogInfo.setStmtdt(tblCSRLog.getStmtdt());
				csrLogInfo.setStmtid(tblCSRLog.getStmtid());
				csrLogInfo.setUserfield1(tblCSRLog.getUserfield1());
				csrLogInfo.setUserfield2(tblCSRLog.getUserfield2());
				csrLogInfo.setUserfield3(tblCSRLog.getUserfield3());
				csrLogInfo.setUserfield4(tblCSRLog.getUserfield4());
				csrLogInfo.setUserid(tblCSRLog.getUserid()); 

				listAuditlogInfo.add(csrLogInfo);
				
			}
			// set paging
			listAuditlogInfo.setPagingInfo(listCSRLog.getPagingInfo());
		}
		return listAuditlogInfo;
	}
}
